package com.itawei.TCPpractise.num2;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

/**
 * 客户端：多次发送数据   接收服务端反馈的消息并打印
 * 服务器：接收多次接收数据 并打印 回写数据到客户端
 */
public class Client {
    public static void main(String[] args) throws IOException {
        //1.创建一个Socket对象 发送的地址
        Socket socket = new Socket("127.0.0.1", 10086);
        Scanner sc = new Scanner(System.in);
        //2.获取输出流
        OutputStream os = socket.getOutputStream();
        while (true) {
            System.out.println("请输入要发送的信息：");
            String s = sc.next();
            if("886".equals( s)){
                break;
            }
            byte[] bytes = s.getBytes();
            os.write(bytes);
        }
        //打印一个标记
        socket.shutdownOutput();
        //接收信息
        InputStream is = socket.getInputStream();
        //转成字符流
        InputStreamReader isr = new InputStreamReader(is);
        int b;
        while((b = isr.read()) != -1){
            System.out.print((char)b);
        }

        //3.释放资源
        os.close();


    }
}
